//
//  ByProductSkuCollectionView.m
//  hutouben_ios
//
//  Created by pro on 16/11/30.
//  Copyright © 2016年 mc. All rights reserved.
//

#import "ByViewDetailSkuCollection.h"

@interface ByViewDetailSkuCollection ()

@property (nonatomic, strong) UIScrollView *scroll;

@property (nonatomic, strong) UILabel *priceLabel;
@property (nonatomic, strong) UILabel *countLabel;

@end

@implementation ByViewDetailSkuCollection

#pragma mark - 懒加载
- (UIScrollView *)scroll {
    
    if (!_scroll) {
        _scroll = [[UIScrollView alloc] initWithFrame:self.bounds];
        _scroll.backgroundColor = [UIColor whiteColor];
        [self addSubview:_scroll];
    }
    return _scroll;
}

- (UILabel *)countLabel {
    
    if (!_countLabel) {
        _countLabel = [[UILabel alloc] init];
        _countLabel.userInteractionEnabled = YES;
        _countLabel.font = FONT(14);
        _countLabel.text = @"1";
        _countLabel.textColor = [UIColor blackColor];
        _countLabel.textAlignment = NSTextAlignmentCenter;
        _countLabel.layer.cornerRadius = 4;
        _countLabel.layer.borderColor = ZJ_SEPLINE_COLOR.CGColor;
        _countLabel.layer.borderWidth = 1;
    }
    return _countLabel;
}
- (UILabel *)priceLabel {
    
    if (!_priceLabel) {
        _priceLabel = [[UILabel alloc] init];
        _priceLabel.userInteractionEnabled = YES;
        _priceLabel.font = FONT(16);
        _priceLabel.text = @"￥0.00";
        _priceLabel.textColor = BY_THEME_COLOR;
        _priceLabel.textAlignment = NSTextAlignmentRight;
    }
    return _priceLabel;
}

#pragma mark - init
- (instancetype)initWithFrame:(CGRect)frame {
    
    if (self = [super initWithFrame:frame]) {
        
    }
    return self;
}

- (void)setDataSource:(id<ByProductSkuCollectionViewDataSource>)dataSource {
    
    _dataSource = dataSource;
    
    [self setupUI];
}
- (void)setupUI {
    
    CGFloat x, y, width, height;
    CGFloat margin = 10;
    // 总组数
    NSInteger sections = [self.dataSource numOfSections:self];
    
    for (int section = 0; section < sections; section ++) {
        
        // 该组的元素数
        NSInteger items = [self.dataSource numOfItems:self inSection:section];
    
        x = 0;
        y = 0;
        width = self.width;
        height = 0;
        UIView *basaelView = [[UIView alloc] initWithFrame:CGRectMake(x, y, width, height)];
        basaelView.top = self.scroll.subviews.lastObject.bottom;
        [self.scroll addSubview:basaelView];
        
        // 如果有实现标题代理，则创建标题视图
        BOOL hasTitle = NO;
        if ([self.dataSource respondsToSelector:@selector(collectionView:titleInSection:)]) {
            
            hasTitle = YES;
            
            NSString *title = [self.dataSource collectionView:self titleInSection:section];
            width = basaelView.width;
            height = 30;
            x = margin;
            y = 0;
            UILabel *titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(x, y, width, height)];
            titleLabel.adjustsFontSizeToFitWidth = YES;
            titleLabel.font = FONT(15);
            titleLabel.text = title;
            titleLabel.textColor = [UIColor blackColor];
            titleLabel.textAlignment = NSTextAlignmentLeft;
            [basaelView addSubview:titleLabel];
        }
        
        CGFloat line = 0;
        CGFloat xOffset = margin;
        for (int item = 0; item < items; item ++) {
            
            NSIndexPath *indexPath = [NSIndexPath indexPathForItem:item inSection:section];
            CGSize size = [self.dataSource collectionView:self sizeForviewAtIndexPath:indexPath];
            UIView *view = [self.dataSource collectionView:self viewAtIndexPath:indexPath];
            width = size.width;
            height = size.height;
            // 需要换行了
            if (xOffset + width > basaelView.width - margin) {
                xOffset = margin;
                line ++;
            }
            y = (hasTitle ? 30 : 0) + (height + margin) * line;
        
            // 设置frame
            view.frame = CGRectMake(xOffset, y, width, height);
            [basaelView addSubview:view];
            UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapAction:)];
            [view addGestureRecognizer:tap];
            view.tag = section * 100 + item * 10;
            
            xOffset += (width + margin);
        }
        
        basaelView.height = basaelView.subviews.lastObject.bottom + margin;
    }
    
    
    // 有规格，创建线
    UIView *line;
    if (sections != 0) {
        // 线
        x = 0;
        y = self.scroll.subviews.lastObject.bottom + 10;
        width = self.scroll.width;
        height = 1;
        line = [[UIView alloc] initWithFrame:CGRectMake(x, y, width, height)];
        line.backgroundColor = ZJ_SEPLINE_COLOR;
        [self.scroll addSubview:line];
    }
    
    
//    // 创建单价视图
//    x = 10;
//    y = line.bottom + 20;
//    width = 60;
//    height = 30;
//    UILabel *priceTitleLabel = [[UILabel alloc] initWithFrame:CGRectMake(x, y, width, height)];
//    priceTitleLabel.font = FONT(16);
//    priceTitleLabel.text = @"单价：";
//    priceTitleLabel.textColor = [UIColor blackColor];
//    priceTitleLabel.textAlignment = NSTextAlignmentLeft;
//    [self.scroll addSubview:priceTitleLabel];
    
//    x = 10;
//    y = priceTitleLabel.top;
//    width = self.scroll.width / 2;
//    height = 30;
//    self.priceLabel.frame = CGRectMake(x, y, width, height);
//    self.priceLabel.text = [NSString stringWithFormat:@"￥%.2lf", [self.price floatValue]];
//    [self.scroll addSubview:self.priceLabel];
//    self.priceLabel.right = self.scroll.width - 10;
    
    // 创建数量视图
    x = 10;
    y = line.bottom + 20;
    width = 120;
    height = 30;
    UILabel *countTitleLabel = [[UILabel alloc] initWithFrame:CGRectMake(x, y, width, height)];
    countTitleLabel.font = FONT(16);
    countTitleLabel.text = @"购买数量";
    countTitleLabel.textColor = [UIColor blackColor];
    countTitleLabel.textAlignment = NSTextAlignmentLeft;
    [self.scroll addSubview:countTitleLabel];
    
    x = 10;
    y = countTitleLabel.top;
    width = self.scroll.width / 3;
    height = 30;
    self.countLabel.frame = CGRectMake(x, y, width, height);
    [self.scroll addSubview:self.countLabel];
    self.countLabel.right = self.scroll.width - 10;
    
    [self.countLabel.subviews makeObjectsPerformSelector:@selector(removeFromSuperview)];
    
    if (self.countLabel.bottom + 20 > self.scroll.height) {
        self.scroll.contentSize = CGSizeMake(0, self.countLabel.bottom + 20);
    }
    
    x = 0;
    y = 0;
    width = self.countLabel.height;
    height = height;
    UIButton *deleteBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    deleteBtn.frame = CGRectMake(x, y, width, height);
    deleteBtn.titleLabel.font = B_FONT(25);
    [deleteBtn setTitle:@"-" forState:UIControlStateNormal];
    [deleteBtn setTitleColor:BY_THEME_COLOR forState:UIControlStateNormal];
    [deleteBtn setTitleColor:[UIColor lightGrayColor] forState:UIControlStateDisabled];
    [deleteBtn addTarget:self action:@selector(delete) forControlEvents:UIControlEventTouchUpInside];
    [self.countLabel addSubview:deleteBtn];
    deleteBtn.tag = 100;
    deleteBtn.enabled = self.countLabel.text.integerValue != 1;
    
    x = 0;
    y = 0;
    width = self.countLabel.height;
    height = height;
    UIButton *addBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    addBtn.frame = CGRectMake(x, y, width, height);
    addBtn.titleLabel.font = B_FONT(25);
    [addBtn setTitle:@"+" forState:UIControlStateNormal];
    [addBtn setTitleColor:BY_THEME_COLOR forState:UIControlStateNormal];
    [addBtn setTitleColor:[UIColor lightGrayColor] forState:UIControlStateDisabled];
    [addBtn addTarget:self action:@selector(add) forControlEvents:UIControlEventTouchUpInside];
    [self.countLabel addSubview:addBtn];
    addBtn.right = self.countLabel.width;
    addBtn.tag = 200;
        
}


- (void)delete {
    
    NSString *countStr = self.countLabel.text;
    NSInteger count = countStr.integerValue;
    NSString *newCountStr = [NSString stringWithFormat:@"%li", --count];
    self.countLabel.text = newCountStr;
    
    UIButton *deleteBtn = [self.countLabel viewWithTag:100];
    deleteBtn.enabled = count != 1;
    
    if ([self.dataSource respondsToSelector:@selector(collectionView:didModifyCount:)]) {
        
        [self.dataSource collectionView:self didModifyCount:^NSInteger{
            
            return count;
        }];
    }
}
- (void)add {
    
    NSString *countStr = self.countLabel.text;
    NSInteger count = countStr.integerValue;
    NSString *newCountStr = [NSString stringWithFormat:@"%li", ++count];
    self.countLabel.text = newCountStr;
    
    UIButton *deleteBtn = [self.countLabel viewWithTag:100];
    deleteBtn.enabled = count != 1;
    
    if ([self.dataSource respondsToSelector:@selector(collectionView:didModifyCount:)]) {
        
        [self.dataSource collectionView:self didModifyCount:^NSInteger{
            
            return count;
        }];
    }
}
- (void)tapAction:(UITapGestureRecognizer *)tap {
    
    NSInteger tag = tap.view.tag;
    NSIndexPath *indexPath = [NSIndexPath indexPathForItem:(tag % 100) / 10 inSection:tag / 100];
    [self.dataSource collectionView:self didSelectItemAtIndexPath:indexPath];
}

- (void)reloadData {
    
    [self.scroll.subviews makeObjectsPerformSelector:@selector(removeFromSuperview)];
    [self setupUI];
}
@end
